import scrapy
from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import CrawlSpider, Rule


class RunoobspiderSpider(CrawlSpider):
    name = 'runoobSpider'
    allowed_domains = ['runoob.com']
    start_urls = ['http://runoob.com/']

    rules = (
        Rule(
            LinkExtractor(allow=r'https://www.runoob.com/[a-z]+[0-9]*[a-z]*/[a-z]+[0-9]*[a-z]*[\-]*[a-z]*.html'),
            callback='parse_item',
            follow=False),
    )

    def parse_item(self, response):
        item = {}
        item['title'] = response.xpath('//*[@id="content"]/h1').xpath('string(.)').extract_first()
        item['introduction'] = response.xpath('//*[@id="content"]/div/p').xpath('string(.)').extract()
        print(item)
        return item
